var upload;
var form;
<!--    关闭当前页面-->
function close(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

//点击编辑按钮图片呈现
var imageUrl = localStorage.getItem("imageUrl");
console.log(imageUrl);
$('#imageUrl').attr("src",imageUrl); // img标签呈现图片
$('input[name="imageUrl"]').val(imageUrl); //input框中（imageUrl）的值赋为图片的路径

//    点击编辑视频加载
//从前端页面获取视频路径
var videoUrl = localStorage.getItem("videoUrl");
console.log(videoUrl);
// $('#videoUrl').attr("href","/images/"+videoUrl); // img标签呈现图片  在上面的html代码先写死
//视频路径放入input框中
$('input[name="videoUrl"]').val(videoUrl); //input框中（imageUrl）的值赋为图片的路径
//视频路径放入视频播放标签的src
$('#videoUrl').attr("src",videoUrl);

//下拉框  图片类型 进行判断
var imageType1 = localStorage.getItem("imageType");
console.log("下拉框的值："+imageType);
if(imageType1 == "图片"){
    $("#imageType").val(imageType1);
}

//表单单选框显示表格值
var item1 = localStorage.getItem("isOnline");
console.log("单选框的值:"+item1);
if(item1 == '下线'){
    $('input[name="isOnline"][value="'+item1+'"]').prop("checked","checked");
}else if (item1 == '上线'){
    $('input[name="isOnline"][value="'+item1+'"]').prop("checked","checked");
}

var s = localStorage.getItem("startTime");
var s1 = localStorage.getItem("expireTime");
//加载组件
layui.use(['form','element','laydate','layer','upload'],function (){
    var laydate = layui.laydate,
        element = layui.element,
        layer = layui.layer,
        form = layui.form;
    upload = layui.upload;
    /**
     * 日期时间  开始日期不得大于结束日期
     开始时间设置
     */
    start = laydate.render({
        elem:'#startTime',//绑定容器
        max:$('#expireTime').val(),
        type: 'datetime',
        value:$('#startTime').val(),
        done:function (value,date){
            if (value !== '') {
                end.config.min.year = date.year;
                end.config.min.month = date.month - 1;
                end.config.min.date = date.date;
                end.config.max.minute = date.minute;
                end.config.max.time = date.time;
                end.config.max.second = date.second;
            } else {
                end.config.min.year = '';
                end.config.min.month = '';
                end.config.min.date = '';
                end.config.max.minute = '';
                end.config.max.time = '';
                end.config.max.second = '';
            }
        }
    })
//结束时间设置
    end = laydate.render({
        elem:'#expireTime',
        min:$('#startTime').val(),
        type:'datetime',
        value: $('#expireTime').val(),
        done:function (value,date){
            if(value != ''){
                start.config.max.year = date.year;
                start.config.max.month = date.month - 1;
                start.config.max.date = date.date;
                start.config.max.minute = date.minute;
                start.config.max.time = date.time;
                start.config.max.second = date.second;
            }else {
                start.config.max.year = '';
                start.config.max.month = '';
                start.config.max.date = '';
                start.config.max.minute = '';
                start.config.max.time = '';
                start.config.max.second = '';
            }
        }
    })


//    更改图片
    var uploadInst = upload.render({
        elem: '#imageupdate'
        // ,url: '/manager/advertiding/uploadImages' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,url:'/tp/upload'
        ,accept:'file'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                console.log("新传的图片路径："+result);
                $('#imageUrl').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功的一些操作
            //……
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传成功</span>');
            var fileUpdate = $('.image');
            fileUpdate.attr("value",res.src);//
            console.log("新传的图片的value值"+fileUpdate.attr("value"));
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //    上传视频
    var uploadInst = upload.render({
        elem: '#videoUpdate'
        , url: '/intermediate/upload'
        , accept: 'video',//视频
        before:function (e) {
            var progressLayer = layer.open({
                type: 0,
                title: false,
                closeBtn: 0,
                btn: false,
                content: '<div class="layui-progress layui-progress-big" lay-filter="progress" lay-showpercent="true"><div class="layui-progress-bar"></div></div>'
            })
            var scanTime = 1000;
            var timer = setInterval(function (){
                $.ajax({
                    url: "/intermediate/getprocess",
                    success: function (data) {
                        console.log(data);
                        element.progress('progress', data +'%')
                        if(data  == 100){
                            //进度到100%，注意关闭定时器
                            clearInterval(timer);
                            //关闭弹出层
                            layer.close(progressLayer);
                            layer.msg('保存成功')
                        }
                    },
                    error: function (e) {
                        //关闭定时器
                        clearInterval(timer);
                        //关闭弹出层
                        layer.close(progressLayer);
                        layer.msg("保存失败");
                        layer.closeAll();
                    }
                });
            }, scanTime);
        }
        , done: function (res) {//上传后的回调
            console.log(res.path);
            $("#videoUrl").attr("src", res.path);
            $("#video").val(res.path);
            $("#time").val(res.time);
            layer.msg('上传成功');
        }
        , error: function () {
            //请求异常回调
            layer.msg('上传失败');
        }

    })

    //编辑页面 确定键
    $('#qd').on('click',function (){
        var val= form.val("formdemo");
        console.log("form表单编辑后的所有值："+val);
        bj(val);
    })


})
//点击取消键返回表格页面
$('#cancel').click(function (){
    // layer.msg("");
    alert("没有改┗|｀O′|┛ 嗷~~！");
    close();
})
//编辑功能
function bj(d){
    $.ajax({
        type:'post',
        url:'/manager/advertiding/update',
        data:d,
        dataType:'json',
        success:function (obj){
            //    关闭当前弹出框
            close();
            console.log(obj.message);
            parent.refresh();

        },
        error:function (e){
            layer.msg("失败");
            close();
        }
    })
}


